@page "/_Admin/FrameworkRole/PageFunction/{id}"
@using WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkRoleVMs
@inherits BasePage

<ValidateForm @ref="vform" Model="@Roles" OnValidSubmit="@Submit">
    <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Inline">
        <Display @bind-Value="@Roles.Entity.RoleCode" ShowLabel="true" />
        <Display @bind-Value="@Roles.Entity.RoleName" ShowLabel="true" />
    </Row>

    @if (Roles?.Pages != null)
    {
        <Table Items="Roles.Pages" TItem="Page_View" ChildrenColumnName="abc" IsPagination="false" IsStriped="true" IsBordered="true" ShowRefresh="false"
               ShowDefaultButtons="false" style="margin-top:10px;">
            <TableColumns>
                <TableColumn @bind-Field="@context.Name" Width="200" />
                <TableColumn @bind-Field="@context.AllActions" Width="800" AllowTextWrap="true">
                    <Template Context="value">
                        @{
                            var row = (Page_View)value.Row;
                            @if (row.IsFolder == false)
                            {
                                @foreach (var item in value.Value)
                                {
                                    var dic = new Dictionary<string, List<Guid>>();
                                    dic.Add(item.Value.ToString(), row.Actions);
                                    <Checkbox style="margin-left:10px;"
                                              State=" row.Actions.Contains(new Guid(item.Value.ToString())) ?CheckboxState.Checked:CheckboxState.UnChecked"
                                              TValue="Dictionary<string,List<Guid>>"
                                              @bind-Value="dic"
                                              ShowLabel="false"
                                              OnStateChanged="OnItemChanged"
                                              DisplayText="@item.Text" ShowAfterLabel="true"></Checkbox>
                                }
                            }
                        }
                    </Template>
                </TableColumn>
            </TableColumns>
        </Table>
    }
    <div class="modal-footer table-modal-footer">
        <Button Color="Color.Primary" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose" />
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Edit"]" IsAsync="true" />
    </div>
</ValidateForm>
<style>
    .table-cell {
        display: inline;
    }
</style>
@code {
    private FrameworkRoleVM Model = new FrameworkRoleVM();
    private FrameworkRoleMDVM2 Roles = new FrameworkRoleMDVM2();
    private ValidateForm vform { get; set; }
    [Parameter]
    public string id { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var rv = await WtmBlazor.Api.CallAPI<FrameworkRoleMDVM2>($"/api/_FrameworkRole/GetPageActions/{id}");
        Roles = rv.Data;
    }


    private async Task Submit(EditContext context)
    {
        await PostsForm(vform, $"/api/_FrameworkRole/EditPrivilege", (s) => "Sys.OprationSuccess", method: HttpMethodEnum.PUT);
    }

    public void OnClose()
    {
        CloseDialog();
    }

    private Task OnItemChanged(CheckboxState state, Dictionary<string, List<Guid>> dic)
    {
        if (dic != null)
        {
            var key = dic.Keys.FirstOrDefault();

            if (state == CheckboxState.UnChecked)
            {
                if (dic[key].Contains(new Guid(key)))
                {
                    dic[key].Remove(new Guid(key));
                }
            }
            if (state == CheckboxState.Checked)
            {
                if (!dic[key].Contains(new Guid(key)))
                {
                    dic[key].Add(new Guid(key));
                }
            }
        }
        return Task.CompletedTask;
    }

}
